import request from '@/utils/request'
import type {
    AddDepartmentParams,
    CommonResponse,
    DeleteDepartmentParams,
    DepartmentResponse,
    DepartmentResponse2,
    UpdateDepartmentParams
} from './types/department'
import {BaseResponse} from "@/api/types/baseResponse";

// 获取部门列表
export function getDepartmentList() {
    return request.get<DepartmentResponse>('/account/department')
}

// 新增部门
export function addDepartment(params: AddDepartmentParams) {
    const formData = new FormData()
    formData.append('pid', params.pid.toString())
    formData.append('dept_name', params.dept_name)
    formData.append('selfid', params.selfid.toString())

    return request.post<CommonResponse>('/account/department', formData)
}

// 编辑部门
export function updateDepartment(params: UpdateDepartmentParams) {
    const formData = new FormData()
    formData.append('dept_name', params.dept_name)
    formData.append('selfid', params.selfid.toString())

    return request.put<CommonResponse>('/account/department', formData)
}

// 删除部门
export function deleteDepartment(params: DeleteDepartmentParams) {
    const formData = new FormData()
    formData.append('selfid', params.selfid.toString())

    return request.delete<CommonResponse>('/account/department', {data: formData})
}

// 查询所有部门
export async function getDepartments() {
    return await request<
        BaseResponse<{ info_list?: DepartmentResponse2[] }>,
        BaseResponse<{ info_list?: DepartmentResponse2[] }>
    >({
        url: "/account/department"
    })
}
